<template>
  <div class="wrapper example9-container">
    <!-- 返回区域 -->
    <div
      class="back-box"
      @click="$router.go(-1)"
    >返回</div>

    <!-- 标题区域 -->
    <h1 class="title-box">9、watchEffect函数</h1>

    <!-- 实例区域 -->
    <div class="example9-box">
      <h1>当前求和为：{{ sum }}</h1>
      <button @click="sum++">点我+1</button>
      <hr>
      <h1>姓名：{{ person.name }}</h1>
      <h1>年龄：{{ person.age }}</h1>
      <h1>薪水：{{ person.job.j1.salary }}K</h1>
      <button @click="person.name+='~'">修改姓名</button>
      <button @click="person.age++">修改年龄</button>
      <button @click="person.job.j1.salary++">涨薪</button>
    </div>
  </div>
</template>

<script>
import { ref, reactive, watchEffect } from 'vue';

export default {
  name: 'Example9',
  setup() {
    // 数据
    const sum = ref(0);
    const person = reactive({
      name: '张三',
      age: 20,
      job: {
        j1: {
          salary: 20
        }
      }
    });

    // watchEffect监视
    watchEffect(() => {
      const x1 = sum.value;
      const x2 = person.job.j1.salary;
      console.log('watchEffect所指明的回调函数执行了--', x1);
      console.log('watchEffect所指明的回调函数执行了--', x2);
    });

    return {
      sum,
      person
    };
  }
};
</script>

<style lang="scss" scoped>
.example9-container {
  // 返回区域
  & > .back-box {
    width: 50px;
    height: 23px;
    line-height: 20px;
    text-align: center;
    background-color: #efefef;
    margin-bottom: 20px;
    font-size: 14px;
    border: 1px solid #767676;
    border-radius: 4px;
    cursor: pointer;
  }

  // 标题区域
  & > .title-box {
    margin-bottom: 30px;
  }

  // 实例区域
  & > .example9-box {
    & > h1 {
      margin-bottom: 20px;
    }
    & > button {
      margin-right: 20px;
      cursor: pointer;
    }
  }
}
</style>
